﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccess;

namespace QUANLYKHACHSAN_N4
{
    public partial class frmQuanLyThietBi : DevComponents.DotNetBar.Office2007Form
    {
        public frmQuanLyThietBi()
        {
            InitializeComponent();
        }
        private void frmQuanLyThietBi_Load(object sender, EventArgs e)
        {
            tabAll_Click(null, null);
            XuLyDataGridView.Style(dgrDanhSach);
        }
        private void tabAll_Click(object sender, EventArgs e)
        {
            TabConTrol.SelectedTab = tabAll;
            dgrDanhSach.DataSource = ThietBi_info.All();
            tabView.Visible = false;
            TabConTrol.Height = 26;
        }
        private void tabThem_Click(object sender, EventArgs e)
        {
            tabView.Visible = false;
            TabConTrol.Height = 119;
            this.tabThem.AttachedControl = this.tabpnInfo;
            btnLamLai_Click(null, null);
            btnSua.Enabled = btnXoa.Enabled = false;
            btnThem.Enabled = true;
        }
        private void tabTimKiem_Click(object sender, EventArgs e)
        {
            tabView.Visible = false;
            TabConTrol.Height = 100;
            switch (cbChoose.SelectedIndex)
            {
                case 0:
                    dgrDanhSach.DataSource = NhanVien_Info.GetByName(txtSearch.Text);
                    break;
                case 1:
                    dgrDanhSach.DataSource = NhanVien_Info.GetByID(txtSearch.Text);
                    break;
                case 2:
                    dgrDanhSach.DataSource = NhanVien_Info.GetByCMND(txtSearch.Text);
                    break;
            }

        }
        private void tabView_Click(object sender, EventArgs e)
        {
            this.tabView.AttachedControl = this.tabpnInfo;
            tabView.Visible = true;
            TabConTrol.SelectedTab = tabView;
            TabConTrol.Height = 119;
            btnSua.Enabled = btnXoa.Enabled = true;
            btnThem.Enabled = false;
        }
        void BinGidToForm(DataGridViewRow r)
        {
            try
            {
                txtMaTB.Text = r.Cells["matb"].Value.ToString();
                txtTenTB.Text = r.Cells["tentb"].Value.ToString();
                txtGia.Text = r.Cells["gia"].Value.ToString();
            }
            catch (Exception)
            {
            }
        }
        private void btnLamLai_Click(object sender, EventArgs e)
        {
            try
            {

                if (!tabView.Visible)
                {
                    txtMaTB.Text =
                    txtTenTB.Text =
                    txtGia.Text = "";
                }
                else
                    BinGidToForm(dgrDanhSach.CurrentRow);
            }
            catch (Exception)
            {
            }
        }
        private void dgrDanhSach_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                BinGidToForm(dgrDanhSach.CurrentRow);
                tabView_Click(null, null);
            }
            catch (Exception)
            {
            }
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            if (ThietBi_info.Add( txtTenTB.Text, txtGia.Text))
            {
                BatLoi.ThongBao("Thêm Thiết Bị Thành Công");
                tabAll_Click(null, null);
            }
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (ThietBi_info.Delete(txtMaTB.Text))
            {
                BatLoi.ThongBao("Xóa Thiết Bị Thành Công");
                tabAll_Click(null, null);
            }

        }
        private void btnSua_Click(object sender, EventArgs e)
        {
            if (!BatLoi.SDT(txtGia, "Vui lòng kiểm tra giá"))
                return;
            if (ThietBi_info.Update(txtMaTB.Text, txtTenTB.Text, txtGia.Text))
            {
                BatLoi.ThongBao("Update Nhân Viên Thành Công");
                tabAll_Click(null, null);
            }
            else
                BatLoi.ThongBao("Update Nhân Viên Thất Bại");

        }

        private void btnXoaSelectItem_Click(object sender, EventArgs e)
        {
            if (BatLoi.DgResult("Bạn có muốn xóa những thiết bị được chọn"))
                foreach (DataGridViewRow r in dgrDanhSach.SelectedRows)
                    ThietBi_info.Delete(r.Cells["matb"].Value.ToString());
        }

        private void txtGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = BatLoi.NhapSo(sender, e);            
        }

    }
} 
